<?php
namespace jy_commission;
defined('BY_JYA') or exit('error');
/*
	极差分红计算	
*/
class jicha extends \plugin{
	/*
		生成分红
	*/
	function create($order,$isbuhuo=false){
		$this->order=$order;
		$this->isbuhuo=$isbuhuo;
		$this->commission_order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$this->order['mid'],
			'ordersn'=>$this->order['ordersn'],
			'order_price'=>$this->order['price'],
			'plugin_name'=>$this->order['plugin_name']?:'',
			'commission_money'=>0,
			'area_money'=>0,
			'xuni_money'=>0,
			'jifen'=>0,
			'red_money'=>0,
			'createtime'=>TIMESTAMP,
		);
		$member=$this->get('member',array('mid'=>$order['mid']));
		$agent_id=$member['agent_id'];
		if($member['level_id']){
			$nowlevel=$this->getcolumn('level',array('id'=>$member['level_id']),'level');
		}else{
			$nowlevel=0;
		}
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid']),
			'orderby'=>'level asc',
			'limit'=>'1',
		);		
		$levels=$this->get_list('level',$parameter);
		$minlevel=$levels[0]['level'];
		$parameter['orderby']='level desc';
		$levels=$this->get_list('level',$parameter);
		$this->commission_order['maxlevel']=$maxlevel=$levels[0]['level'];
		$levelnum=12;
		//极差分红单线等级ABCDE，第一级没有分红，低于自己的没有分红，平级没有分红
		for($i=0;$i<$levelnum;$i++){
				if(!$agent_id){
					break;
				}
				$member=$this->get('member',array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
				$agent_id=$member['agent_id'];
				$level=$this->get('level',array('id'=>$member['level_id']));
				$level['level']=$level['level']?:0;
				//上等级不是最大级，当前等级小于上等级或小于最小等级，都没有分红	
				if($nowlevel!=$maxlevel && ($level['level']<$minlevel || $level['level']<=$nowlevel)){
					continue;
				}
				//当等级匹配时，分红
				$nowlevel=$level['level'];
				$this->create_redcommission($member,$level);
				if($agent_id==$member['mid']){
					break;
				}
		}
		//生成分销订单
		if($this->commission_order['isarea'] && !$this->order['goods']){
			$indata=$this->commission_order;
			unset($indata['isarea']);
			unset($indata['maxlevel']);
			unset($indata['red_money']);
			$this->insert('order',$indata);
		}
		return true;
	}
	/*
		级差分佣type=1;
	*/
	function create_redcommission($agent,$level=array(),$i=0){
			if(!$agent['level_id']){
				//获取默认等级分红比例
				$level_commissions=$this->plugin_setting('default_level');
				$level_id=0;
				$commission_per=$level_commissions['ji_rate'];
			}else{
				$level['commissions']=json_decode($level['commissions'],true);
				$level_id=$level['id'];
				$commission_per=$level['commissions']['ji_rate'];
			}
			$money=0;
			$order_price=$this->commission_order['order_price'];
			$remark=array(
				'level_id'=>$level_id,
				'order_price'=>$order_price,
				'goods_commission'=>array(),
			);
			if($this->order['goods']){
			foreach($this->order['goods'] as $goods){
					$total_price=$goods['total_price']?$goods['total_price']:pdo_getcolumn('jy_weishop_order_goods',array('id'=>$goods['id']),'total_price');
					$goods_commission=$this->get('goods',array('goods_id'=>$goods['goodsid']));
					if(!$goods_commission['id']){
						$shopgoods=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('pcate','costprice'));
						$goods_commission=$this->get('goods',array('goods_pcate'=>$shopgoods['pcate'],'uniacid'=>$_SESSION['uniacid']));
					}
					if($goods_commission['ignore'] || !$goods_commission['id']){
						if($goods_commission['ignore']){
							$order_price=$order_price-$total_price;
						}
						//只要有一个商品，没有独立设置分销，就订单计算佣金
						$this->needorder=true;
						continue;
					}
					if($this->isbuhuo){
						$buhuocheck=model('jy_buhuo/model')->uptotal($agent['mid'],$goods['goodsid'],$goods['total'],$this->order['plugin_name']);
						if(!$buhuocheck){
							$order_price=$order_price-$goods['total_price'];
							continue;
						}
					}
					//如果商品单独设置佣金，将减少本商品的金额
					$order_price=$order_price-$total_price;
					$goods_commission['commissions']=$goods_commission['commissions']?json_decode($goods_commission['commissions'],true):array();
					foreach($goods_commission['commissions'] as $gc){
						if($gc['level_id']==0){
							$demogoods_commission_per=$gc['ji_rate'];
						}
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_commission_per=$gc['ji_rate'];
							break;
						}
					}
					$goods_commission_per=$goods_commission_per?:$demogoods_commission_per;
					$goods_commission_per=$goods_commission_per?:$commission_per;	
					if(substr($goods_commission_per,-1)=='%'){
						$goods_commission_money=($total_price*substr($goods_commission_per,0,-1))/100;
					}else{
						$goods_commission_money=$goods_commission_per*$goods['total'];
					}
					$goods_commission_money -=$this->commission_order['commission_money'];
					$money+=$goods_commission_money;
			}
			}
			if($order_price>0 || ($this->order['goods'] && $this->needorder)){
				if($this->isbuhuo && $this->order['video_id']){
					$buhuocheck=model('jy_buhuo/model')->uptotal($agent['mid'],$this->order['video_id'],1,$this->order['plugin_name']);
					if(!$buhuocheck){
						return $agent['agent_id'];
					}
				}
				$order_commission_money=intval($order_price*$commission_per)/100;
				$order_commission_money -=$this->commission_order['commission_money'];
				$money+=$order_commission_money;
				$remark['order_commission']=array('order_price'=>$order_price,'commission_per'=>$commission_per,'commission_money'=>$order_commission_money);
			}
			$remark['commission_money']=$money;
			if($money>0){
				$this->commission_order['commission_money']+=$money;
				$new_commission=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$agent['mid'],
					'level'=>$i,
					'ordersn'=>$this->order['ordersn'],
					'money'=>$money,
					'jifen'=>$jifen?:0,
					'remark'=>ijson_encode($remark),
					'createtime'=>TIMESTAMP,
					'status'=>$this->setting['isbuy_jiesuan']?1:0,
					'type'=>20,
				);
				$this->insert('commission',$new_commission);
			}
			//判断是否存在团队长
			$this->commission_order['isarea']=true;
			//返回上级
			return $agent['agent_id'];
	}
}